﻿using CatUtils.Utils.Currency;
using CatUtilsDll.Utils.Currency.WebUtils;
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace CatUtilsDll.Utils.Currency.TextUtils
{
    /// <summary>
    /// 编码转码
    /// </summary>
    public class Transcoding
    {
        /// <summary>
        /// Url编码 默认UTF8
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string UrlEncode(string Url)
        {
            PrintLog.VNoWrite("Transcoding.UrlEncode已废弃，请使用：WebHelper.UrlEncode");
            return WebHelper.UrlEncode(Url, Encoding.UTF8);
        }

        /// <summary>
        /// Url编码
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="encoding">自行设定编码格式</param>
        /// <returns></returns>
        public static string UrlEncode(string Url, Encoding encoding)
        {
            PrintLog.VNoWrite("Transcoding.UrlEncode已废弃，请使用：WebHelper.UrlEncode");
            return System.Web.HttpUtility.UrlEncode(Url, encoding);
        }

        /// <summary>
        ///UrlEncode 编码
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [Obsolete]
        public string UrlEncodeToUtf8(string Url)
        {
            PrintLog.VNoWrite("Transcoding.UrlEncode已废弃，请使用：WebHelper.UrlEncode");
            return UrlEncode(Url);
        }

        /// <summary>
        /// Url解密
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string UrlDeCode(string Url)
        {
            PrintLog.VNoWrite("Transcoding.UrlDeCode，请使用：WebHelper.UrlDeCode");
            return UrlDeCode(Url, Encoding.UTF8);
        }

        /// <summary>
        /// Url解密  自行设定解密模式
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string UrlDeCode(string Url, Encoding encoding)
        {
            PrintLog.VNoWrite("Transcoding.UrlDeCode，请使用：WebHelper.UrlDeCode");
            return System.Web.HttpUtility.UrlDecode(Url, encoding);
        }

        /// <summary>
        ///UrlDecode 解码
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [Obsolete]
        public string UrlDecodeToUtf8(string Url)
        {
            PrintLog.VNoWrite("Transcoding.UrlDeCode，请使用：WebHelper.UrlDeCode");
            return UrlDeCode(Url);
        }

        /// <summary>
        /// Html解码
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public static string HtmlDecode(string Html)
        {
            PrintLog.VNoWrite("Transcoding.HtmlDecode，请使用：HtmlHelper.Decode");
            return System.Web.HttpUtility.HtmlDecode(Html);
        }

        /// <summary>
        ///HtmlDecode 解码
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        [Obsolete]
        public string HtmlDecodeToUtf8(string Text)
        {
            PrintLog.VNoWrite("Transcoding.HtmlDecode，请使用：HtmlHelper.Decode");
            return HtmlDecode(Text);
        }

        /// <summary>
        ///  Html编码
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public string HtmlEncode(string Html)
        {
            PrintLog.VNoWrite("Transcoding.HtmlEncode，请使用：HtmlHelper.Encode");
            return System.Web.HttpUtility.HtmlEncode(Html);
        }

        /// <summary>
        /// HtmlDecode 解码
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        [Obsolete]
        public string HtmlEncodeToUtf8(string Text)
        {
            PrintLog.VNoWrite("Transcoding.HtmlEncode，请使用：HtmlHelper.Encode");
            return System.Web.HttpUtility.HtmlEncode(Text);
        }

        /// <summary>
        /// Unicode编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string EnUnicode(string str)
        {
            StringBuilder strResult = new StringBuilder();
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    strResult.Append("\\u");
                    strResult.Append(((int)str[i]).ToString("x"));
                }
            }
            return strResult.ToString();
        }

        /// <summary>
        /// Unicode解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DeUnicode(string str)
        {
            //最直接的方法Regex.Unescape(str);
            Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
            return reg.Replace(str, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
        }
    }
}